// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// l [author] rhyme_lph
// q [email]  rhymelph@gmail.com
// U [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/entities/bank_detail_record_item_entity.dart';
import 'package:finance_app/src/entities/bank_reconciliation_record_entity.dart';
import 'package:finance_app/src/entities/reconciliation_item_entity.dart';
import 'package:finance_app/src/http_service/http_bank_reconciliations_service.dart';
import 'package:finance_app/src/provider/all_reports/bank_reconciliation/bank_reconciliation_provider.dart';
import 'package:finance_app/src/provider/all_reports/bank_reconciliation/views/reconcile_account_provider.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/widgets/bottom_sheet/delete_info_bottom_sheet.dart';
import 'package:flutter/material.dart';

class ReconcileAccountConfirmProvider extends BaseProvider {
  int reconcileId;
  List<BankReconciliationRecordEntity> records; //with file
  List<BankDetailRecordItemEntity> selectReconcileList; //no file
  List<int> paymentIds;

  num get reconciledDeposits {
    num total = 0;
    if (records != null) {
      for (final item in records) {
        if (item.total >= 0 && item.status == 'closed') {
          total += item.total;
        }
      }
    }

    if (selectReconcileList != null) {
      for (final item in selectReconcileList) {
        if (item.total >= 0 && item.flowType == "in") {
          total += item.total;
        }
      }
    }
    return total;
  }

  num get reconciledWithdrawals {
    num total = 0;
    if (records != null) {
      for (final item in records) {
        if (item.total <= 0 && item.status == 'closed') {
          total += item.total;
        }
      }
    }

    if (selectReconcileList != null) {
      for (final item in selectReconcileList) {
        if (item.total >= 0 && item.flowType == "out") {
          total += item.total;
        }
      }
    }
    return total;
  }

  @override
  Future get dataFuture => null;

  void onConfirm() async {
    ReconciliationItemEntity orderEntity;
    List<int> paymentIds;
    if (selectReconcileList != null) {
      paymentIds = selectReconcileList.map((e) => e.id).toList();
    }
    await post(
        HttpBankReconciliationsService.reconcile(reconcileId, paymentIds),
        autoHandler: AutoHandler.Toast,
        loadingTip: S.current.loading, onSuccess: (result) {
      orderEntity = getEntityFromResult<ReconciliationItemEntity>(result);
    });

    if (orderEntity != null) {
      obtainContext?.call((context) async {
        bool isSuccess =
            await showSuccessFullyReconciledBottomSheet(context, onCancel: () {
          Navigator.of(context).pop(false);
        });
        print(isSuccess);
        if (isSuccess == true) {
          Navigator.of(context)..pop()..pop();
        } else if (isSuccess == false) {
          String bankName = Providers.get<BankReconciliationProvider>(
                  RouterGenProviders.bankReconciliationPage, null)
              ?.getChildProvider<ReconcileAccountProvider>()
              ?.currentAccount
              ?.bankName;
          Providers.get<BankReconciliationProvider>(
                  RouterGenProviders.bankReconciliationPage, null)
              ?.getChildProvider<ReconcileAccountProvider>()
              ?.isDestroy = true;
          await Routes.navigateTo(
            RouterGenProviders.dateReportPage,
            params: {
              'type': 76,
              'meta': {
                'bankName': bankName,
                'closingDate': orderEntity.endDate,
                'id': orderEntity.id,
              }
            },
          );
          obtainContext?.call((context) {
            Navigator.of(context).pop();
          });
        }
      });
    }
  }
}
